<?php
class PicSharding extends BaseModelPicSharding {
	public static $sizes = array( 
	                         array('w' => 90, 'h' => 90), 
	                         array('w' => 350, 'h' => 240),
	                         array('w' => 850, 'h' => 450));
	
	public static function batchResize($file) {
		$fileType = exec("file -bi {$file}");
		$fileType = substr($fileType, strlen('image/'), strlen($fileType));
		
		if (!in_array($fileType, array('jpeg', 'png', 'gif'))) {
			return false;
		}
		
		$imk = new imagick($file);
		$_width = $imk->getImageWidth();
		$_height = $imk->getImageHeight();
		for ($i = 0, $len = count(self::$sizes); $i < $len; $i++) {
			$tmp = $file."_".self::$sizes[$i]['w']."_".self::$sizes[$i]['h'];
		    $width = self::$sizes[$i]['w'];
		    $height = self::$sizes[$i]['h'];
			if (self::$sizes[$i]['w'] >= $_width
			         && self::$sizes[$i]['h'] <= $_height) {
			    $height = $_height;	
			}
			else if (self::$sizes[$i]['w'] <= $_width
			    && self::$sizes[$i]['h'] >= $_height) {
			    $width = $_width;	
			}
			else if (self::$sizes[$i]['w'] <= $_width
			    && self::$sizes[$i]['h'] >= $_height) {
				$width = $_width;
				$height = $_height;
			}
			$cmd = "convert -resize {$width}x{$height}! -quality 85 {$file} {$tmp}";
			exec($cmd);
		}	
	}

	public static function setSize($sizes) {
		self::$sizes = $sizes;
	}
	
	public static function getSize() {
		return self::$sizes;
	}
	
	public static function getMostSuitSize(&$width, &$height) {
		$ms = $width;
		$msTag = 0;
		for ($i = 0; $i < count(self::$sizes); $i++) {
			if (abs($width - self::$sizes[$i]['w']) < $ms) {
				$ms = abs($width - self::$sizes[$i]['w']);
				$msTag = $i;
			}
		}
		$width = self::$sizes[$msTag]['w'];
		$height = self::$sizes[$msTag]['h'];
		
		return self::$sizes[$msTag];
	}
	
	public static function seekFile($file, $sourceFile) {
		$isExistF = file_exists($file);
		$isExistSF = file_exists($sourceFile);
		if (!empty($isExistSF) && !empty($isExistF)) {
			return $file;
		}
		else if (empty($isExistSF) && empty($isExistF)) {
			echo "file not exits";
			return NULL;
		}
		else if (empty($isExistF) && !empty($isExistSF)) {
			if (empty($width) && empty($height)) {
				return $sourceFile;
			}
			else {
				$suiteSize = PicSharding::getMostSuitSize($width, $height);
				$file = $sourceFile."_".$width."_".$height;
				if (!file_exists($file)) {
					return $sourceFile;
				}
			}
		}
		return NULL;
	}
	
	public static function sendImage($file) {
        $fileMtime = filemtime($file);	
        $expirePeriod = 31104000;
        $expiredMtime = $fileMtime + $expirePeriod;
		$headers = apache_request_headers();	
		header("Cache-Control: max-age=$expirePeriod;");
        header("Content-Encoding: no-gzip;");
		header('Expires: '.gmdate('D, d M Y H:i:s', $expiredMtime).' GMT');
	    
		$fileType = exec("file -bi $file");
		$fileType = substr($fileType, strlen('image/'), strlen($fileType));
		$fileSize = filesize($file);		
		header("Content-Length: ".$fileSize);
		header("Content-Type: $fileType");
		
		if (isset($headers['If-Modified-Since']) && 
		   (strtotime($headers['If-Modified-Since']) == $fileMtime)) {
	        // Client's cache IS current, so we just respond '304 Not Modified'.
	        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $expiredMtime).' GMT', true, 304);
	        exit();
	    }
		
		header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileMtime).' GMT', true, 200);
		
		$fp = fopen($file, "rb");
		if (empty($fp)) {
			echo "open file failed.";
			exit();
		} 
		$fc = fread($fp, $fileSize);
		fclose($fp);
		echo $fc;
	}
}
